home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / jedit42install.exe / {app} / macros / Editing / Greedy_Left.bsh < prev    next >
Encoding:
Text File  |  2004-08-29  |  2.0 KB  |  81 lines

  1. /*
  2.  * Greedy_Left.bsh - If a buffer is using soft tabs,
  3.  * this macro will move the caret tabSize spaces to the left,
  4.  * if all the characters between the caret and the previous
  5.  * tab stop are all spaces.  In all other cases, the caret
  6.  * is moved a single character to the left.
  7.  *
  8.  * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
  9.  *
  10.  * $Id: Greedy_Left.bsh,v 1.3 2004/05/20 12:47:33 orutherfurd Exp $
  11.  */
  12.  
  13. /**
  14.  * @param onlyFullTabs if true, the caret will only be moved
  15.  *                     multiple spaces it would constitute
  16.  *                     a 'complete' tab.
  17.  */
  18. void greedyLeft(View view, boolean onlyFullTabs)
  19. {
  20.     JEditTextArea textArea = view.getTextArea();
  21.     Buffer buffer = textArea.getBuffer();
  22.     int caret = textArea.getCaretPosition();
  23.     int caretLine = textArea.getCaretLine();
  24.     int lineStart = textArea.getLineStartOffset(caretLine);
  25.  
  26.     if(textArea.getCaretPosition() == 0)
  27.         return;
  28.  
  29.     if(buffer.getBooleanProperty("noTabs") == true)
  30.     {
  31.         // if anything is selected, use standard 
  32.         if(textArea.getSelection().length != 0)
  33.         {
  34.             textArea.setCaretPosition(caret-1);
  35.         }
  36.         // if at the start of the line, use standard
  37.         else if(caret == lineStart)
  38.         {
  39.             textArea.setCaretPosition(caret-1);
  40.         }
  41.         else
  42.         {
  43.             int col = caret - lineStart;
  44.             int tabSize = buffer.getIntegerProperty("tabSize",8);
  45.  
  46.             // unlikely, but just in case
  47.             if(tabSize <= 0)
  48.             {
  49.                 textArea.setCaretPosition(caret-1);
  50.             }
  51.             else
  52.             {
  53.                 int toTabStop = ((col-1) % tabSize) + 1;
  54.                 int count = 0;
  55.                 String chunk = buffer.getText(caret-toTabStop,toTabStop);
  56.                 for(int i=0; i < toTabStop; i++)
  57.                 {
  58.                     if(' ' != chunk.charAt(i))
  59.                         break;
  60.                     count += 1;
  61.                 }
  62.  
  63.                 // if onlyFullTabs must be only spaces to
  64.                 // the tabStop and must have tabSize number 
  65.                 // of spaces to remove them all.
  66.                 if(onlyFullTabs == false || count == tabSize){
  67.                     textArea.setCaretPosition(caret-count);
  68.                 }
  69.                 else{
  70.                     textArea.setCaretPosition(caret-1);
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     else
  76.         textArea.setCaretPosition(caret-1);
  77. }
  78.  
  79. greedyLeft(view,true);
  80.  
  81.